﻿using Furion;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Security.Claims;
using VueEleAdminFurion.Model;

namespace VueEleAdminFurion.Controllers.PermissionsServices;

public interface IPermissionsService
{

}

[DynamicApiController]
public class PermissionsService
{
    SqlSugar.ISqlSugarClient db;
    public PermissionsService(ISqlSugarClient db)
    {
        this.db = db;
    }
    public async ValueTask CreatePermissions(Permissions dto)
    {
        dto.Id = Guid.NewGuid();
        dto.CreateUserId = Guid.Parse(input: App.User?.FindFirstValue("UserId"));
        dto.CreateTime = db.GetDate().ToString();
        db.Insertable(dto).IgnoreColumns(ignoreNullColumn: true).IgnoreColumns(x => x.TimeStamp).ExecuteCommand();
    }
    public async ValueTask DeletePermissions([FromQuery] Permissions dto)
    {
        db.Deleteable(dto).ExecuteCommand();
        db.Deleteable<UserPermissions>().Where(it => it.PermissionsId == dto.Id).ExecuteCommand();
        db.Deleteable<RolePermissions>().Where(it => it.PermissionsId == dto.Id).ExecuteCommand();
    }
    public async ValueTask UpdatePermissions(Permissions dto)
    {
        db.Updateable(dto).IgnoreColumns(ignoreAllNullColumns: true).IgnoreColumns(x => x.TimeStamp).ExecuteCommand();
    }
    public async ValueTask<List<Dto.TreePermissionsDto>> GetTreePermissions()
    {
        return db.Queryable<Dto.TreePermissionsDto>().OrderBy(it => it.Sort).ToTree(it => it.Children, it => it.ParentId, null).ToList();
    }
}
